// pages/list/list.js
// 列表页
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId: null,
    submenu: 'zonghe',
    navSearchIcon: '/assets/icons/search.png',
    screenIcon: '/assets/icons/screen.png',
    addressIcon: '/assets/icons/address.png',
    topCategoryId: 0,
    pageTitle: ['网红列表', '摄影师列表', '化妆师列表', '场地列表'],
    currentPageTitle: '',
    topNavList: [],
    categoryIds: [],
    searchResultNavList: [{
        id: 1,
        nav: '综合',
        key: 'zonghe'
      },
      {
        id: 2,
        nav: '最热',
        key: 'zuire'
      },
      {
        id: 3,
        nav: '最新',
        key: 'zuixin'
      }
    ],
    dataList: [],
  },
  clickSearchBtn: function() {
    // 点击搜索跳转搜索页
    wx.navigateTo({
      url: '/pages/homeSearch/homeSearch',
    })
  },
  clickTopNav: function(e) {
    // 点击选中顶部导航
    let topCategoryId = e.currentTarget.dataset.id
    let categoryId = []
    categoryId.push(topCategoryId)
    this.setData({
      categoryIds: categoryId,
      topCategoryId: topCategoryId
    })
    this.getDataList(this.data.navId, this.data.categoryIds)

  },
  // 修改页面标题
  changePageTitle: function() {
    wx.setNavigationBarTitle({
      title: this.data.currentPageTitle,
    })
  },
  clickScreenNav: function(e) {
    //点击筛选导航
    let submenu = e.target.dataset.key
    this.setData({
      submenu: submenu
    })
    let navId = wx.getStorageSync('navid')
    let categoryIds = this.data.categoryIds
    this.getDataList(navId, categoryIds, submenu)
  },
  clickScreen: function() {
    // 点击筛选
    wx.navigateTo({
      url: '/pages/homeScreen/homeScreen',
    })
  },
  clickHomePage: function(e) {
    // 点击打开个人主页
    let id = e.currentTarget.dataset.id
    let currentPageTitle = this.data.currentPageTitle
    wx.navigateTo({
      url: '/pages/homePage/homePage?id=' + id + '&cagetoryName=' + currentPageTitle,
    })
  },
  labelScreen: function(labelList) {
    // 首页筛选标签数量
    if (labelList.length > 2) {
      if (labelList[0].length > 2 || labelList[1].length > 2) {
        return labelList.slice(0, 2)
      }
    }
    return labelList.slice(0, 3)
  },

  getDataList: function(navId, categoryIds, submenu) {
    // 获取dataList数据
    let _this = this
    let config = wx.getStorageSync('config')
    let dataListUrl = config.host + 'api/product/queryAll'
    let data = {
      type: navId,
    }
    if (categoryIds) {
      data.categoryIds = categoryIds
    }
    if (submenu) {
      data[submenu] = '1'
    }

    wx.request({
      url: dataListUrl,
      method: 'POST',
      data,
      success(res) {
        if (res.data.status == 200 && res.data.msg == 'ok') {
          let data = []
          res.data.data.forEach(item => {
            item.label = item.label.split(',')
            item.label = _this.labelScreen(item.label)
            data.push(item)
          })
          _this.setData({
            dataList: data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 修改页面标题
    let navId = options.navId
    let _this = this
    let config = wx.getStorageSync('config')
    this.setData({
      currentPageTitle: this.data.pageTitle[navId - 1],
      navId: navId
    })
    this.changePageTitle()
    // 获取顶部类目导航
    wx.request({
      url: config.host + 'api/product/findCategory',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        type: navId
      },
      success(res) {
        if (res.statusCode == 200 && res.data.msg == 'ok') {
          _this.setData({
            topNavList: res.data.data
          })
        }
      }
    })
    this.getDataList(navId, [], this.data.submenu)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  }
})